﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using pdtransport_v2._0.Models;

namespace pdtransport_v2._0.Areas.back.Controllers
{
    [Authorize(Roles="admin")]
    public class WebServiceController : Controller
    {
        //
        // GET: /back/WebService/
        pdtransportDataContext datacontext = new pdtransportDataContext();
        HelpController help = new HelpController();
        public ActionResult Danhsach(string keyword)
        {
            int total_rows = 0;
            List<WebService> ws;
            if (keyword == null)
            {
                total_rows = (from _ws in datacontext.WebServices
                              select _ws).Count();
            }
            else
            {
                total_rows = (from _ws in datacontext.WebServices
                              where _ws.username.Contains(keyword) == true || _ws.CongTy1.TenCongTy.Contains(keyword) == true
                              select _ws).Count();
            }

            ViewBag.total_row = total_rows;
            string base_URL = "Webservice/DanhSach"; // tên method của controller KhachHangController

            // phan trang
            string URL_segment;
            try
            {
                URL_segment = Request.Url.Segments[4];
            }
            catch (Exception)
            {
                URL_segment = null;
            }

            Libraries.Pagination pagination = new Libraries.Pagination(base_URL, URL_segment, total_rows);
            string pageLinks = pagination.GetPageLinks();
            int start = (pagination.CurPage - 1) * pagination.PerPage;
            int offset = pagination.PerPage;
            if (URL_segment != null)
                pageLinks = pageLinks.Replace(base_URL + "/", "");
            ViewBag.pageLinks = pageLinks;
            // phan trang

            if (keyword == null)
            {
                ws = (from _ws in datacontext.WebServices
                              select _ws).Skip(start).Take(offset).ToList();
            }
            else
            {
                ws = (from _ws in datacontext.WebServices
                      where _ws.username.Contains(keyword) == true || _ws.CongTy1.TenCongTy.Contains(keyword) == true
                      select _ws).Skip(start).Take(offset).ToList();
            }

            return View(ws);
        }

        [HttpGet]
        public ActionResult Them()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Them(string tencongty, string email, string sodt, string fax, string username, string password)
        {
            CongTy congty = new CongTy();
            WebService webservice = new WebService();

            // Thêm công ty
            congty.TenCongTy = tencongty;
            congty.Email = email;
            congty.SoDT = sodt;
            congty.Fax = fax;
            congty.TinhTrang = 1;

            datacontext.CongTies.InsertOnSubmit(congty);
            datacontext.SubmitChanges();

            int idct = datacontext.CongTies.OrderByDescending(_ct => _ct.IDCT).Take(1).Single().IDCT;

            // Thêm Webservice của công ty
            webservice.CongTy = idct;
            webservice.username = username;
            webservice.password = help.Encrypt(password);
            webservice.TinhTrang = 1;

            datacontext.WebServices.InsertOnSubmit(webservice);
            datacontext.SubmitChanges();

            ViewBag.Them = 1;

            return RedirectToAction("DanhSach");
        }

        [HttpGet]
        public ActionResult Sua(int idws)
        {
            WebService ws = (from _ws in datacontext.WebServices
                             where _ws.IDWS == idws
                             select _ws).Single();

            return View(ws);
        }

        [HttpPost]
        public ActionResult Sua(int idws, string email, string sodt, string fax, string password, int tinhtrang)
        {
            WebService ws = (from _ws in datacontext.WebServices
                             where _ws.IDWS == idws
                             select _ws).Single();

            CongTy ct = (from _ct in datacontext.CongTies
                         where _ct.IDCT == ws.CongTy
                         select _ct).Single();

            // cập nhật password lại cho công ty khách hàng
            if (password.Length != 0)
            {
                ws.password = help.Encrypt(password);
            }
            ws.TinhTrang = 1;
            // cập nhật thông tin của công ty
            ct.Email = email;
            ct.Fax = fax;
            ct.SoDT = sodt;

            datacontext.SubmitChanges();

            ViewBag.Sua = 1;

            return View(ws);
        }

        public ActionResult Xoa(int idws)
        {
            WebService ws = (from _ws in datacontext.WebServices
                             where _ws.IDWS == idws
                             select _ws).Single();

            CongTy ct = (from _ct in datacontext.CongTies
                         where _ct.IDCT == ws.CongTy
                         select _ct).Single();

            ws.TinhTrang = 0;
            ct.TinhTrang = 0;

            datacontext.SubmitChanges();

            return RedirectToAction("DanhSach");
        }
    }
}
